package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior>
        implements ApUnlikesBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    /**
     * 存储喜欢数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        //点赞行为信息保存
        if (dto == null || dto.getArticleId() == null ||
                (dto.getType().intValue() !=0 && dto.getType().intValue() != 1) ){
            return ResponseResult.
                    errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN, "未登陆状态,无法进行踩操作");
        }
        //获取实体对象
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
        //查询行为信息关系是否存在
        ApUnlikesBehavior unlikesBehavior = getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery().
                eq(ApUnlikesBehavior::getArticleId, dto.getArticleId()).
                eq(ApUnlikesBehavior::getEntryId,apBehaviorEntry.getId()));
        if (unlikesBehavior == null && dto.getType().intValue() == 1) {
            //如果当前查询到的用户行为关系为空,并且进行的是取消踩操作的话
            return ResponseResult.errorResult(AppHttpCodeEnum.ILLEGAL_OPERATION, "非法操作");
        }

        if (unlikesBehavior!=null&&unlikesBehavior.getType().intValue()  == (short) dto.getType().intValue()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "不可进行重复操作");
        }

        //点赞关系数据表插入操作
        if (unlikesBehavior == null) {
            unlikesBehavior = new ApUnlikesBehavior();
            unlikesBehavior.setArticleId(dto.getArticleId());
            unlikesBehavior.setCreatedTime(new Date());
            unlikesBehavior.setEntryId(apBehaviorEntry.getId());
            unlikesBehavior.setType(dto.getType());
            save(unlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            //4.取消踩操作
            unlikesBehavior.setType(dto.getType());
            updateById(unlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }
}
